<?php


namespace org;

use think\Controller;
use think\Db;
use think\Request;
use OSS\OssClient;
use OSS\Core\OssException;
use org\AliSts;

class AliOss extends Controller{
    private $config = [];
    private $ossClient;

    public function __construct(Request $request = null)
    {
        $storage_config=Db::name('storage')->field('ali')
            ->where('acid',1)
            ->find();
        $this->config = json_decode($storage_config['ali'], true);
        $accessKeyId = $this->config['accessKeyId'];
        $accessKeySecret = $this->config['accessKeySecret'];
        $endpoint = $this->config['oss']['endpoint'];
        // Endpoint以杭州为例，其它Region请按实际情况填写。
        // $endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
        try {
            $this->ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
        } catch (OssException $e) {
            print $e->getMessage();
        }
    }

    /**
     * @return $name 存在Bucket返回 Bucket name，不存在创建后返回
     */
    public function getBucket($name)
    {
        $listbuckets = $this->listBuckets();
        foreach($listbuckets as $bucket) {
            if($bucket->getName() == $name) {
                return [0, $name];
            }
        }
        $result = $this->createBucket($name);
        if($result[0] == 0) {
            return [0, $name];
        }
        return [-1, $result[1]];
    }

    /**
     * 创建存储空间bucket
     * @return mixed
     */
    public function createBucket($name)
    {
        try {
            $options = array(
                OssClient::OSS_STORAGE => OssClient::OSS_STORAGE_IA
            );
            // 设置存储空间的权限为公共读，默认是私有读写。
            $this->ossClient->createBucket($name, OssClient::OSS_ACL_TYPE_PUBLIC_READ, $options);
            return [0, $name];
        } catch (OssException $e) {
            return [-1, $e->getMessage()];
        }
    }

    /**
     * 获取所有存储空间
     * @return mixed
     */
    public function listBuckets()
    {
        try{
            $bucketListInfo = $this->ossClient->listBuckets();
        } catch(OssException $e) {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return [];
        }
        $bucketList = $bucketListInfo->getBucketList();
        return $bucketList;
    }

    /**
     * 简单上传
     * $bucket 存储空间名称
     * $object 文件名
     * $filePath 本地路径
     * @return array [错误号， 文件名/错误信息]
     */
    public function uploadFile($object, $filePath)
    {
//        $bucket= "<yourBucketName>";
//// 设置文件名称。
//        $object = md5(time().uuid()).'.'.$type;
//// <yourLocalFile>由本地文件路径加文件名包括后缀组成，例如/users/local/myfile.txt。
//        $filePath = "<yourLocalFile>";
        $bucket = $this->config['oss']['bucket'];
        $bucket = $this->getBucket($bucket);
        if ($bucket[0] == -1) {
            return $bucket;
        }
        $bucket = $bucket[1];
        try{
            $this->ossClient->uploadFile($bucket, $object, $filePath);
            return [0, $object];
        } catch(OssException $e) {
            return [-1, $e->getMessage()];
        }
    }

    /**
     * 大文件分片上传
     * $bucket 存储空间名称
     * $object 文件名
     * $filePath 本地路径
     * @return array [错误号， 文件名/错误信息]
     */
    public function multiuploadFile($object, $file)
    {
        $bucket = $this->config['oss']['bucket'];
        $bucket = $this->getBucket($bucket);
        if ($bucket[0] == -1) {
            return $bucket;
        }
        $bucket = $bucket[1];
        $options = array(
            OssClient::OSS_CHECK_MD5 => true,
            OssClient::OSS_PART_SIZE => 1024 * 1024 * 10,
        );
        try{
            $this->ossClient->multiuploadFile($bucket, $object, $file, $options);
            return [0, $object];
        } catch(OssException $e) {
            return [-1, $e->getMessage()];
        }
    }

    /**
     * 删除文件
     * @return
     */
    public function deleteObject($object)
    {
        try{
            $bucket = $this->config['oss']['bucket'];
            $this->ossClient->deleteObject($bucket, $object);
            return [0, '删除成功'];
        } catch(OssException $e) {
            return [0, '删除失败，错误信息：'. $e->getMessage()];
        }
    }

    /**
     * 批量删除文件
     * $objects array[$object, $object2, ...]
     * @return
     */
    public function deleteObjects($objects)
    {
        try{
            $bucket = $this->config['oss']['bucket'];
            $this->ossClient->deleteObjects($bucket, $objects);
            return [0, '删除成功'];
        } catch(OssException $e) {
            return [0, '删除失败，错误信息：'. $e->getMessage()];
        }
    }





}